package com.cheng.example.geektime.designpatterns.j60.v4;

//public class OrderService {
//    public double discount(Order order) {
//        double discount = 0.0;
//        OrderType type = order.getType();
//        if (type.equals(OrderType.NORMAL)) { // 普通订单
//            //...省略折扣计算算法代码
//        } else if (type.equals(OrderType.GROUPON)) { // 团购订单
//            //...省略折扣计算算法代码
//        } else if (type.equals(OrderType.PROMOTION)) { // 促销订单
//            //...省略折扣计算算法代码
//        }
//        return discount;
//    }
//}

// 策略的使用
public class OrderService {
    public double discount(Order order) {
        OrderType type = order.getType();
        
        //这个替代了，if的判断
        DiscountStrategy discountStrategy = DiscountStrategyFactory.getDiscountStrategy(type);
        //这个封装了策略算法
        return discountStrategy.calDiscount(order);
    }

    public static void main(String[] args) {
        Order order = new Order();
        order.setType(OrderType.GROUPON);

        OrderService orderService = new OrderService();
        double d = orderService.discount(order);
        System.out.println(d);
    }
}